package main

import (
	"fmt"
	"log"
	"os"
	"sync"
)

func main() {


	file, err := os.Create("db.log")
	if err != nil {
		log.Fatalln("fail to create db.log file!")
	}
	defer file.Close()


	var filename, imgPath string
	filename = os.Args[1]
	imgPath = os.Args[2]

	fmt.Println(filename)

	producer := Producer{
		closedChan: make(chan struct{}),
	}

	sourceDataChan := make(chan *sourceData, 10)
	go producer.Produce(filename, sourceDataChan)

	var wg sync.WaitGroup
	consumerNumber := 10
	wg.Add(consumerNumber)
	var consumers []Consumer
	fmt.Println("h 1")
	for i := 0; i < consumerNumber; i++ {
		consumers = append(consumers, Consumer{
			workNo:  i,
			wg:      &wg,
			imgPath: imgPath,
			logger: file,
		})
	}

	for i := 0; i < consumerNumber; i++ {
		go consumers[i].Work(sourceDataChan, producer.closedChan)
	}

	wg.Wait()

	file.Sync()

	fmt.Println("main over")

}
